// band_withdrawal_account_page.dart
import 'package:flutter/material.dart';
import 'package:sjx/repository/model/withdrawal_model.dart';
import 'package:sjx/utils/sp_utils.dart';
import 'package:sjx/repository/model/user_model.dart';

import '../../repository/sjx_api.dart';
import 'band_withdrawal_edit_page.dart';

class BandWithdrawalAccountPage extends StatefulWidget {
  const BandWithdrawalAccountPage({super.key});

  @override
  State<BandWithdrawalAccountPage> createState() =>
      _BandWithdrawalAccountPageState();
}

class _BandWithdrawalAccountPageState extends State<BandWithdrawalAccountPage> {
  WithdrawalModel? _withdrawalModel;
  bool _isLoading = true;
  String? _errorMessage;

  @override
  void initState() {
    super.initState();
    _loadAccountInfo();
  }

  Future<void> _loadAccountInfo() async {
    try {
      setState(() {
        _isLoading = true;
        _errorMessage = null;
      });

      _withdrawalModel = await SJXApi.instance().userWithdrawAccountInfo();

      setState(() {
        _isLoading = false;
      });
    } catch (e) {
      setState(() {
        _isLoading = false;
        _errorMessage = '加载失败: $e';
      });
    }
  }

  void _navigateToEditPage() {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => BandWithdrawalEditPage(
          withdrawalModel: _withdrawalModel,
          onUpdate: _loadAccountInfo,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('提现账户'),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () => Navigator.of(context).pop(),
        ),
      ),
      backgroundColor: const Color.fromRGBO(244, 247, 250, 1),
      body: _isLoading
          ? _buildLoading()
          : _errorMessage != null
          ? _buildError()
          : _buildContent(),
    );
  }

  Widget _buildLoading() {
    return const Center(
      child: CircularProgressIndicator(),
    );
  }

  Widget _buildError() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            _errorMessage!,
            style: const TextStyle(color: Colors.red),
          ),
          const SizedBox(height: 16),
          ElevatedButton(
            onPressed: _loadAccountInfo,
            child: const Text('重试'),
          ),
        ],
      ),
    );
  }

  Widget _buildContent() {
    return SingleChildScrollView(
      child: Column(
        children: [
          // 支付宝账户卡片
          Container(
            margin: const EdgeInsets.all(16),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(8),
              boxShadow: [
                BoxShadow(
                  color: Colors.black.withOpacity(0.05),
                  blurRadius: 4,
                  offset: const Offset(0, 2),
                ),
              ],
            ),
            child: Material(
              color: Colors.white,
              child: InkWell(
                onTap: _navigateToEditPage,
                child: Container(
                  padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 15),
                  child: Row(
                    children: [
                      // 标题
                      const Text(
                        '支付宝账户',
                        style: TextStyle(
                          fontSize: 16,
                          color: Color(0xFF262626),
                        ),
                      ),
                      const Spacer(),
                      // 状态显示
                      Text(
                        _withdrawalModel?.isBound == true ? '已绑定' : '未绑定',
                        style: TextStyle(
                          fontSize: 14,
                          color: _withdrawalModel?.isBound == true
                              ? const Color(0xFF666666)
                              : const Color(0xFF666666),
                        ),
                      ),
                      const SizedBox(width: 8),
                      // 箭头图标
                      const Icon(
                        Icons.arrow_forward_ios,
                        size: 12,
                        color: Color(0xFF666666),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),

          // 提示信息
          Container(
            margin: const EdgeInsets.symmetric(horizontal: 16),
            padding: const EdgeInsets.all(12),
            decoration: BoxDecoration(
              color: const Color(0xFFE6F7FF),
              borderRadius: BorderRadius.circular(6),
              border: Border.all(
                color: const Color(0xFF91D5FF),
                width: 1,
              ),
            ),
            child: const Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Icon(
                  Icons.info_outline,
                  size: 16,
                  color: Color(0xFF1890FF),
                ),
                SizedBox(width: 8),
                Expanded(
                  child: Text(
                    '绑定支付宝账户后，提现金额将直接转入您的支付宝账户',
                    style: TextStyle(
                      fontSize: 12,
                      color: Color(0xFF1890FF),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}